home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4903 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.4 KB  |  50 lines

  1. Path: newsfeed.gsfc.nasa.gov!usenet
  2. From: Dirk Broer <Dirk.Broer@gsfc.nasa.gov>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help! Allocating 3 dimensional or greater arrays in C++
  5. Date: Sun, 28 Jan 1996 11:07:29 -0800
  6. Organization: NASA Goddard Space Flight Center -- Greenbelt, Maryland USA
  7. Message-ID: <310BC971.D3B@gsfc.nasa.gov>
  8. References: <310CD09F.1D71@ns.vvm.com>
  9. NNTP-Posting-Host: control.gsfc.nasa.gov
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b5 (Win16; I)
  14.  
  15. Arun Nair wrote:
  16. > I have been trying to allocate a 3 dimensional array in C++.
  17. > void main()
  18. > {
  19. >         int *matrix;
  20. >         int **pmatrix = &matrix;
  21. >         int ***ppmatrix = &pmatrix;
  22.  
  23. why are you initializing pmatrix and ppmatrix?
  24.  
  25. >         int size;
  26. >         matrix = new int[10];
  27. >         pmatrix = new matrix[10];
  28. >         ppmatrix = new pmatrix[10];
  29.  
  30. To make an i x j x k matrix you must allocate i x j x k data points pluse j 
  31. x k pointers.  It is not necessary to allocate these linearly (ie in one 
  32. block) unless you code specifically requires it.
  33.  
  34.     typedef int*** Matrix;
  35.     typedef int** pMatrix;
  36.     typedef int* ppMatrix;
  37.  
  38.     Matrix    matrix;
  39.     int  a,b;
  40.  
  41.     matrix = new Matrix[i];
  42.     for( a = 0; a < i; n++ )
  43.     {
  44.          matrix[n] = new pMatrix[j];
  45.          for( b =0; b<j; b++ )
  46.               matrix[a][b] = new ppMatrix[k];
  47.     }
  48.